<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

//get the new module version from xml file.
$module = 'album';

//update to 1.1.0
$version = "1.1.0";

//compare it with the installed module version 

if ($this->system->modules[$module]['version'] < $version)
{
	
	$this->load->dbforge();
	$fields = array (
			'id' => array(
				'type' => 'INT',
				'auto_increment' => TRUE,
			),
			'aid' => array(
				 'type' => 'VARCHAR',
				 'constraint' => 20,
				 'default' => ''
			),
			'gid' => array(
				'type' => 'INT',
				'default' => '0'
			)
		);
	$this->dbforge->add_field($fields); 
	$this->dbforge->add_key('id', TRUE);
	$this->dbforge->add_key('aid');
	$this->dbforge->add_key('gid');
	$this->dbforge->create_table('album_groups', TRUE);

		
	$this->session->set_flashdata("notification", sprintf(__("Album module updated to %s", $module), $version)) ;
	
	$data = array('version' => $version);
	$this->db->where(array('name'=> $module));
	$this->db->update('modules', $data);
	redirect("admin/module");
}

//update to 1.2.0
$version = "1.2.0";

//compare it with the installed module version 

if ($this->system->modules[$module]['version'] < $version)
{
	$this->db->query("ALTER TABLE " . $this->db->dbprefix('album_categories') . " ADD `g_id` VARCHAR( 20 ) NOT NULL DEFAULT '0'");
	$query = $this->db->get('album_groups');
	if($query->num_rows() > 0)
	{
		foreach ($query->result_array() as $row)
		{
			$this->db->where('aid', $row['aid']);
			$this->db->set('g_id', $row['gid']);
			$this->db->update('album_categories');
		}
	}

	$this->session->set_flashdata("notification", sprintf(__("Album module updated to %s", $module), $version)) ;
	
	$data = array('version' => $version);
	$this->db->where(array('name'=> $module));
	$this->db->update('modules', $data);
	redirect("admin/module");
}

//update to 1.3.0
$version = "1.3.0";

//Notify also those who commented

if ($this->system->modules[$module]['version'] < $version)
{
	$this->db->query("ALTER TABLE " . $this->db->dbprefix('album_image_comments') . " ADD `notify` INT( 1 ) NOT NULL DEFAULT '1'");

	$this->session->set_flashdata("notification", sprintf(__("Album module updated to %s", $module), $version)) ;
	
	$data = array('version' => $version);
	$this->db->where(array('name'=> $module));
	$this->db->update('modules', $data);
	redirect("admin/module");
}

//update to 1.4.0
$version = "1.4.0";

//cover is a link not an sid

if ($this->system->modules[$module]['version'] < $version)
{
	$this->db->query("ALTER TABLE " . $this->db->dbprefix('album_categories') . " CHANGE `cover` `cover` VARCHAR( 255 )");
	$query = $this->db->get_where('album_categories', array('cover <>' => ''));
	if($query->num_rows() > 0)
	{
		foreach ($query->result_array() as $row)
		{
			$this->db->select('s');
			$query2 = $this->db->get_where('album_images', array('sid' => $row['cover']));
			if($query2->num_rows() > 0)
			{
				$row2 = $query2->row_array();
				$this->db->set('cover' , $row2['s']);
				$this->db->where('aid' , $row['aid']);
				$this->db->update('album_categories');
			}
		}
	}

	$this->session->set_flashdata("notification", sprintf(__("Album module updated to %s", $module), $version)) ;
	
	$data = array('version' => $version);
	$this->db->where(array('name'=> $module));
	$this->db->update('modules', $data);
	redirect("admin/module");
}

//update to 1.5.0
$version = "1.5.0";

//cover was not fixed so do it again

if ($this->system->modules[$module]['version'] < $version)
{
	$this->db->query("ALTER TABLE " . $this->db->dbprefix('album_categories') . " CHANGE `cover` `cover` VARCHAR( 255 )");
	
	$this->db->limit(50);
	$query = $this->db->get('album_categories');
	if($query->num_rows() > 0)
	{
		
		foreach ($query->result_array() as $row)
		{
			$this->db->select('s');
			$this->db->limit(1);
			$this->db->where('aid', $row['aid']);
			$query2 = $this->db->get('album_images');
			if($query2->num_rows() > 0)
			{
				$row2 = $query2->row_array();
				$this->db->set('cover' , $row2['s']);
				$this->db->where('aid' , $row['aid']);
				$this->db->update('album_categories');
			}
		}
		$this->cache->remove_group('album_list');
	}

	$this->session->set_flashdata("notification", sprintf(__("Album module updated to %s", $module), $version)) ;
	
	$data = array('version' => $version);
	$this->db->where(array('name'=> $module));
	$this->db->update('modules', $data);
	redirect("admin/module");
}

//update to 1.5.1
$version = "1.5.1";

//album list was limited to 20

if ($this->system->modules[$module]['version'] < $version)
{
	$this->cache->remove_group('album_list');
	$this->session->set_flashdata("notification", sprintf(__("Album module updated to %s", $module), $version)) ;
	
	$data = array('version' => $version);
	$this->db->where(array('name'=> $module));
	$this->db->update('modules', $data);
	redirect("admin/module");
}

//update to 1.6.0
$version = "1.6.0";

//translation

if ($this->system->modules[$module]['version'] < $version)
{
	$this->db->query("CREATE TABLE " . $this->db->dbprefix('album_infos') ." (`id` INT NOT NULL auto_increment, `album` VARCHAR( 20 ) NOT NULL , `lang` VARCHAR( 2 ) NOT NULL , `title` VARCHAR( 255 ) NOT NULL , `description` TEXT NOT NULL , PRIMARY KEY ( `id` ) , INDEX ( `album`), INDEX( `lang` ) )");
	
	$this->cache->remove_group('album_list');
	$this->session->set_flashdata("notification", sprintf(__("Album module updated to %s", $module), $version)) ;
	
	$data = array('version' => $version);
	$this->db->where(array('name'=> $module));
	$this->db->update('modules', $data);
	redirect("admin/module");
}

//update to 1.6.1
$version = "1.6.1";

//adding ordering field

if ($this->system->modules[$module]['version'] < $version)
{
	$this->db->query("ALTER TABLE " . $this->db->dbprefix('album_categories') . " ADD `ordering` INT( 11 ) NOT NULL DEFAULT 0");
	$this->session->set_flashdata("notification", sprintf(__("Album module updated to %s", $module), $version)) ;
	
	$data = array('version' => $version);
	$this->db->where(array('name'=> $module));
	$this->db->update('modules', $data);
	redirect("admin/module");
}

$version = "1.6.2";

//when trying to open a reserved album, show the login form

if ($this->system->modules[$module]['version'] < $version)
{
	$this->session->set_flashdata("notification", sprintf(__("Album module updated to %s", $module), $version)) ;
	
	$data = array('version' => $version);
	$this->db->where(array('name'=> $module));
	$this->db->update('modules', $data);
	redirect("admin/module");
}
